package 算法.NiuKe.JZ;

import 算法.NiuKe.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;

/**
 * @author:谢君臣
 * @Date:2021/3/2115:35
 * @version:1.0
 * @function:
 * 从上往下打印出二叉树的每个节点，同层节点从左至右打印。
 */
public class JZ22 {
    public static void main(String[] args) {

    }
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        Queue<TreeNode> treeNodes = new ArrayDeque<>();
        ArrayList<Integer> res = new ArrayList<>();
        if (root==null)
            return res;
        treeNodes.add(root);
        while (!treeNodes.isEmpty()){
            TreeNode node = treeNodes.poll();
            if (node.left!=null)
                treeNodes.add(node.left);
            if (node.right!=null)
                treeNodes.add(node.right);
            res.add(node.val);
        }
        return res;
    }
    public int[] levelOrder(TreeNode root) {
        Queue<TreeNode> treeNodes = new ArrayDeque<>();
        ArrayList<Integer> res = new ArrayList<>();
        if (root==null)
            return new int[0];
        treeNodes.add(root);
        while (!treeNodes.isEmpty()){
            TreeNode node = treeNodes.poll();
            if (node.left!=null)
                treeNodes.add(node.left);
            if (node.right!=null)
                treeNodes.add(node.right);
            res.add(node.val);
        }
        int[] a = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            a[i] = res.get(i);
        }
        return a;
    }
}
